import React, { Component } from 'react'
import '../../assets/index.css'

import Tabs from './components/tabs'
import List from './components/list'
import Form from './components/form'
class App extends Component {
  state = {
    // 文本框内容
    texts: '',
    // hot: 热度排序  time: 时间排序
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot',
      },
      {
        id: 2,
        name: '时间',
        type: 'time',
      },
    ],
    // 当前选中的 tab 的type 值
    active: 'hot',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-10-10 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '哎哟，不错哦',
        time: new Date('2021-10-11 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
      },
      {
        id: 3,
        author: '五月天',
        comment: '不打扰，是我的温柔',
        time: new Date('2021-10-11 10:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
      },
    ],
  }

  //tab 栏切换
  changeTabName = (type) => {
    this.setState({ ...this.state, active: type })
  }

  // 发布
  publish = (texts) => {
    const newList = [...this.state.list]
    newList.unshift({
      id: this.state.list.length + 1,
      author: '作者',
      comment: texts,
      time: new Date(),
      // 1: 点赞 0：无态度 -1:踩
      attitude: 0,
    })
    this.setState({
      list: newList,
    })
  }
  // 删除评论
  delLi = (id) => {
    console.log(id)
    const newList = this.state.list.filter((item) => {
      return item.id !== id
    })
    this.setState({ list: newList })
  }
  // 点赞
  clickedUp = (id, attitude) => {
    console.log(attitude)
    this.setState({
      list: this.state.list.map((item) => {
        if (item.id === id) {
          return {
            ...item,
            attitude: attitude === 1 ? 0 : 1,
          }
        } else {
          return item
        }
      }),
    })
  }
  // 踩
  clickedDown = (id, attitude) => {
    console.log(attitude)
    this.setState({
      list: this.state.list.map((item) => {
        if (item.id === id) {
          return {
            ...item,
            attitude: attitude === -1 ? 0 : -1,
          }
        } else {
          return item
        }
      }),
    })
  }
  render() {
    return (
      <div className="App">
        <div className="comment-container">
          {/* 评论数 */}
          <div className="comment-head">
            <span>{this.state.list.length} 评论</span>
          </div>
          {/* 排序 */}
          <Tabs
            tabs={this.state.tabs}
            active={this.state.active}
            changeTabName={this.changeTabName}
          />

          {/* 添加评论 */}
          <Form publish={this.publish} />
          {/* 评论列表 */}
          <List
            list={this.state.list}
            formatTime={this.formatTime}
            clickedUp={this.clickedUp}
            clickedDown={this.clickedDown}
            delLi={this.delLi}
          />
        </div>
      </div>
    )
  }
}
export default App
